package org.bot.client.storage;

import com.alibaba.fastjson.JSONObject;
import org.bot.api.event.DataStorage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapDataStorage implements DataStorage {

    private final Map<String, Object> map = new HashMap<>();

    @Override
    public void put(String index, Object data) {
        this.map.put(index, data);
    }

    @Override
    public Object get(String index) {
        return this.map.get(index);
    }

    @Override
    public <T> List<T> getSource(Class<T> t) {
        List<T> list = new ArrayList<>();
        map.forEach((k, v) -> {
            if (v == null) {
                return;
            }
            if (t.isInstance(v)) {
                list.add(t.cast(v));
            }
        });
        return list;
    }

    @Override
    public boolean containsKey(String index) {
        return map.containsKey(index);
    }

    @Override
    public JSONObject toJSONObject() {
        return new JSONObject(this.map);
    }

}
